﻿<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
class b2c_ctl_wap_passport extends wap_frontpage{
    function __construct(&$app){
        parent::__construct($app);
        $this->_response->set_header('Cache-Control', 'no-store');
        kernel::single('base_session')->start();
        $this->userObject = kernel::single('b2c_user_object');
        $this->userPassport = kernel::single('b2c_user_passport');
    }


/*add by peter 2017-05-17 issue 308 校验验证码*/
public function ajax_check_code(){
$vcode=$_POST['vcode'];
if(!base_vcode::verify('LOGINVCODE',$vcode)){
	echo '验证码填写错误.';
}
}


//add by lkb 2017-05-19
//验证账号
public function ajax_check_username(){
if($this->app->getConf('site.sms_only_valide')=="true"){
            $flag = $this->userPassport->check_signup_account_pc_mobile( trim($_POST['username']),$msg );
        } else{
            $flag = $this->userPassport->check_signup_account( trim($_POST['username']),$msg );
        }
        if($flag){
        }else{
            echo $msg;exit;
        }

}

//密码
public function ajax_check_login_password(){
    $passwdlen = strlen( trim($_POST['login_password']) );

        if(!preg_match('/^(([0-9]+)|([a-z]+)|([A-Z]+))*$/i', trim($password)) ){
            $msg = $this->app->_('该登录密码包含非法字符');
            echo $msg;exit;

        }

        if($passwdlen<6){
            $msg = $this->app->_('密码长度不能小于6位');
            echo $msg;exit;
        }
        if($passwdlen>20){
            $msg = $this->app->_('密码长度不能大于20位');
            echo $msg;exit;
        }
    }
    //重复密码
public function ajax_check_psw_confirm(){
    $passwdlen = strlen( trim($_POST['login_password']) );
    $psw_confirm = strlen( trim($_POST['psw_confirm']) );

        if($psw_confirm != $passwdlen){
            $msg = $this->app->_('输入的密码不一致');
            echo $msg;exit;
        }
    }


//法人姓名
public function ajax_check_frname(){
        $flag = $this->userPassport->check_frname($_POST,$msg);
        if(!$flag){
            echo $msg;exit;
          // $this->splash('error',null,$msg,true);exit;
        }
    }

    //法人身份证
public function ajax_check_idnumber(){
        $flag = $this->userPassport->check_idnumber($_POST,$msg );
        
        if(!$flag){
             echo $msg;exit;
          // $this->splash('error',null,$msg,true);exit;

        }
    }

    //法人身份证
public function ajax_check_gcname(){
        $flag = $this->userPassport->check_gcname($_POST,$msg );
        
        if(!$flag){
           echo $msg;exit;
        }
    }

    //业务员名称
    public function ajax_check_salesman(){
            
            $salesman=$_POST['salesman'];
         if($salesman!=null){
                if( strlen(trim($salesman))< 4 ){
                    $msg = $this->app->_('业务员姓名长度最少4个字符');
                    echo $msg;exit;
                }
                elseif( strlen($salesman)>100 ){
                    $msg = $this->app->_('业务员姓名过长，请换一个重试');
                    echo $msg;exit;
                }

                if( is_numeric($salesman) ){
                    $msg = $this->app->_('业务员姓名不能全为数字');
                    echo $msg;exit;
                }

                if(!preg_match('/^[^\x00-\x2d^\x2f^\x3a-\x3f]+$/i', trim($salesman)) ){
                    $msg = $this->app->_('该业务员姓名包含非法字符');
                    echo $msg;exit;
                }
         }
        

            //查找会员名称
        //     $pam_members_model = app::get('pam')->model('members');
        // $members_data = $pam_members_model->getList('*',array('login_account'=>trim($_POST['salesman'])));

        //     if(count($members_data))
        //     {
                
        //     }
        //     else{
        //         $msg="不存在此业务员，请重新输入";
        //      echo $msg;exit;
        //     }
    }

     //联系电话
public function ajax_check_telephone(){
         $flag = $this->userPassport->check_signup_account_pc_tel( trim($_POST['telephone']),$msg );
        if($flag){
           
        }else{
             echo $msg;exit;
        }
    }
//add end
    /*
     * 如果是登录状态则直接跳转到会员中心
     * */
    public function check_login($mini){
        if( $this->userObject->is_login() )
        {
            $url = $this->gen_url(array('app'=>'b2c','ctl'=>'wap_member','act'=>'index'));
            if($_GET['mini_passport']==1 || $mini){
                kernel::single('wap_router')->http_status(302);return;
            }else{
                //您已经是登录状态，不需要重新登录
                $this->redirect($url);
            }
        }
        return false;
    }

    public function index(){
        //如果会员登录则直接跳转到会员中心
        $this->check_login();
        $this->login();
    }

    /*
     * 登录view
     * */
    public function login($mini=0){
        //如果会员登录则直接跳转到会员中心
        $this->check_login($mini);

        $flag = false;
        if($_GET['mini_passport']==1 || $mini) {
            $flag = true;
            $this->pagedata['mini_passport'] = 1;
        }

        //是否开启验证码
        $this->pagedata['show_varycode'] = kernel::single('b2c_service_vcode')->status();

        #//信任登录openapi
        foreach(kernel::servicelist('openid_imageurl') as $object){
            if(is_object($object)){
                if(method_exists($object,'get_image_url')){
                    $login_image_urls[] = $object->get_image_url();
                }
            }
        }
        #$this->pagedata['login_image_url'] = $login_image_url;
        $data = app::get('trustlogin')->getConf('trustlogin_rule');

        // print_r($data);
        //信任登录trustlogin
        if($data['status'] && $data['status'] == 'true')
        {
            foreach(kernel::servicelist('trustlogin_trustlogin.wap_trustlogin_cfg') as $object){
                if(is_object($object)){
                    if(method_exists($object,'get_logo')){
                        $login_image_url[] = $object->get_logo();
                    }
                }
            }

                //信任登录trustlogin判断后台启用状态
            foreach ($login_image_url as $key => $value)
            {
                if(!$value['status'])
                {
                    unset($login_image_url[$key]);
                }
                if($value['status'] && $value['status'] == 'false')
                {
                    unset($login_image_url[$key]);
                }
            }
        }

        if($login_image_url)
        {
            $this->pagedata['login_image_url'] = $login_image_url;
        }
        if($login_image_urls)
        {
            $this->pagedata['login_image_urls'] = $login_image_urls;
        }

// print_r($login_image_url);
// print_r('-------------');
// print_r($login_image_urls);
        $this->pagedata['loginName'] = $_COOKIE['loginName'];

        $this->userPassport->set_next_page('mobile');
        $this->set_tmpl('passport');
        $this->page('wap/passport/index.html');
    }//end function

    /**
     * 检查登录账号是否需要开启手机验证
     */
    public function login_ajax_account(){
        $login_account = trim($_POST['uname']);
        if( $this->userPassport->check_login_account($login_account,$msg) ){
            echo json_encode(array('needVerify'=>'true'));exit;
        }else{
            echo json_encode(array('needVerify'=>'false'));exit;
        }
    }

    //微信绑定
    public function loginbind(){
        if( $_SESSION['weixin_u_openid'] ){
            $bind_tag = app::get('pam')->model('bind_tag')->getRow('member_id',array('open_id'=>$_SESSION['weixin_u_openid']));
            if( $bind_tag ){
                $msg = app::get('b2c')->_('您已绑定，不需要重新绑定');
                $this->splash('failed',null,$msg);exit;
            }
        }
        $this->unset_member();
        $_SESSION['is_bind_weixin'] = true;
        $this->login();
    }

    public function bindstatus(){
        if( $_SESSION['weixin_u_openid'] ){
            $bind_tag = app::get('pam')->model('bind_tag')->getRow('tag_type,tag_name,id',array('open_id'=>$_SESSION['weixin_u_openid']));
            if( !$bind_tag ){
                $url = $this->gen_url(array('app'=>'b2c','ctl'=>'wap_passport','act'=>'loginbind'));
                $this->redirect($url);
            }else{
                $bind_tag['tag_name'] = kernel::single('weixin_wechat')->emoji_decode($bind_tag['tag_name']);
                $this->pagedata['name'] = $bind_tag['tag_name'] ? $bind_tag['tag_name'] : $bind_tag['tag_type'].'_'.$bind_tag['id'];
                $this->page('wap/bind/bindstatus.html');
            }
        }else{
            $url = $this->gen_url(array('app'=>'b2c','ctl'=>'wap_passport','act'=>'login'));
            $this->redirect($url);
        }
    }

    public function unbindweixin($newbind=false){
        $bind_tag = app::get('pam')->model('bind_tag')->delete(array('open_id'=>$_SESSION['weixin_u_openid']));
        if( $newbind ){
            $this->unset_member();
            $url = $this->gen_url(array('app'=>'b2c','ctl'=>'wap_passport','act'=>'loginbind'));
            $this->redirect($url);
        }else{
            $msg = app::get('b2c')->_('解除绑定成功');
            $this->splash('success',null,$msg);exit;
        }
    }

    /*
     * 登录验证
     * */
    public function post_login(){
        //_POST过滤
        $userData = array(
            'login_account' => $_POST['uname'],
            'login_password' => $_POST['password']
        );

        //是否需要进行手机验证
        if( !kernel::single('b2c_user_vcode')->mobile_login_verify($_POST['mobileVcode'],$_POST['uname'],'activation')){
            $msg = app::get('b2c')->_('手机短信验证码错误');
            $this->splash('failed',null,$msg,'','',true);exit;
        }

        if(kernel::single('b2c_service_vcode')->status() && empty($_POST['verifycode'])){
            $msg = app::get('b2c')->_('请输入验证码!');
            $this->splash('failed',null,$msg,'','',true);exit;
        }

        $member_id = kernel::single('pam_passport_site_basic')->login($userData,$_POST['verifycode'],$msg);
        if(!$member_id){
            //设置登录失败错误次数 一个小时三次错误后需要自动开启验证码
            kernel::single('b2c_service_vcode')->set_error_count();
            $this->splash('failed',null,$msg,'','',true);exit;
        }

        $b2c_members_model = $this->app->model('members');
        $member_point_model = $this->app->model('member_point');

        $member_data = $b2c_members_model->getList( 'member_lv_id,experience,point', array('member_id'=>$member_id) );
        if(!$member_data){
            kernel::single('b2c_service_vcode')->set_error_count();
            //在登录认证表中存在记录，但是在会员信息表中不存在记录
            $msg = $this->app->_('登录失败：会员数据存在问题,请联系商家或客服');
            $this->splash('failed',null,$msg,'','',true);exit;
        }

        $member_data = $member_data[0];
        $member_data['order_num'] = $this->app->model('orders')->count( array('member_id'=>$member_id) );

        if($this->app->getConf('site.level_switch')==1)
        {
            $member_data['member_lv_id'] = $b2c_members_model->member_lv_chk($member_data['member_lv_id'],$member_data['experience']);
        }
        if($this->app->getConf('site.level_switch')==0)
        {
            $member_data['member_lv_id'] = $member_point_model->member_lv_chk($member_id,$member_data['member_lv_id'],$member_data['point']);
        }

        $b2c_members_model->update($member_data,array('member_id'=>$member_id));
        $this->userObject->set_member_session($member_id);
        kernel::single('pam_lock')->flush_lock($member_id);
        $this->set_cookie('loginName',$_POST['uname'],time()+31536000);//用于记住用户名
        if($_POST['site_autologin'] == 'on'){
            $minutes = 14*24*60;
            $this->set_cookie('autologin',$minutes,time()+31536000);
        }
        $this->app->model('cart_objects')->setCartNum();

        $bindOpenId = app::get('pam')->model('bind_tag')->getRow('member_id',array('open_id'=>$_SESSION['weixin_u_openid']));
        $bindMember = app::get('pam')->model('bind_tag')->getRow('member_id',array('member_id'=>$member_id));
        if( $_SESSION['weixin_u_openid'] && !$bindOpenId && !$bindMember ){
            $bindWeixinData = array(
                'member_id' => $member_id,
                'open_id' => $_SESSION['weixin_u_openid'],
                'tag_name' => kernel::single('weixin_wechat')->emoji_encode($_SESSION['weixin_u_nickname']),
                'createtime' => time()
            );
            $flag = app::get('pam')->model('bind_tag')->save($bindWeixinData);
            $this->bind_member($member_id); //绑定完微信之后，在创建cookie
            $url = kernel::single('wap_frontpage')->gen_url(array('app'=>'b2c','ctl'=>'wap_passport','act'=>'weixin_bind_view','arg0'=>$flag));
            $this->splash('success',$url,'','','',true);
        }else{
            $url = $this->userPassport->get_next_page('mobile');
            if( !$url ){
                $url = kernel::single('wap_frontpage')->gen_url(array('app'=>'b2c','ctl'=>'wap_member','act'=>'index'));
            }
            if( $_SESSION['weixin_u_openid'] && $bindMember && $_SESSION['is_bind_weixin'] ){
                $this->unset_member();
                unset($_SESSION['is_bind_weixin']);
                $this->splash('error',null,app::get('b2c')->_('该账号绑定过其他微信账号,不能再进行绑定,请先解除绑定,或再直接登录'),'','',true);
            }else{
                $this->bind_member($member_id); //绑定完微信之后，在创建cookie
                $this->splash('success',$url,app::get('b2c')->_('登录成功'),'','',true);
            }
        }
    }//end function

    /**
     * 绑定成功或失败提示页面
     */
    public function weixin_bind_view($status=true){
        $this->pagedata['status'] = $status ? 'true' : 'false';
        $this->pagedata['sso_setting'] = app::get('weixin')->getConf('weixin_sso_setting');
        $this->page('wap/bind/bind_tag.html');
    }


    //注册页面
    public function signup($url=null){
        //检查是否登录，如果已登录则直接跳转到会员中心
        $this->check_login();

        $this->begin();
        //是否启用会员注册
        $register = app::get('b2c')->getConf('system.member.register');
        if ($register==='false') {
            $this->end(false,app::get('b2c')->_('抱歉，当前不能注册，请联系管理员！'));exit;
        }

        $this->userPassport->set_next_page('mobile');

        $this->pagedata['mini_passport'] = $_GET['mini_passport'];

        //获取会员注册项
        $this->pagedata['attr'] = $this->userPassport->get_signup_attr();
        $this->pagedata['site_sms_valide'] = $this->app->getConf('site.sms_valide');

        $this->set_tmpl('passport');
        //注册是否需要验证码
        $this->pagedata['valideCode'] = app::get('b2c')->getConf('site.register_valide');

        $this->set_tmpl('passport');

        if($_GET['referrals_code']){
            $_SESSION['referrals_code']=$_GET['referrals_code'];
        }

        $this->page("wap/passport/signup.html",$_GET['mini_passport']);
    }

    public function license(){
        $this->pagedata['reg_license'] = app::get('wap')->getConf('wap.register.license');
        $this->page('wap/passport/license.html');
    }

    //注册的时，检查账号
    public function signup_ajax_check_name(){
        $flag = $this->userPassport->check_signup_account( trim($_POST['pam_account']['login_name']),$msg );
        if($flag){
            if($msg == 'mobile'){
                echo json_encode(array('needVerify'=>'true'));exit;
            }
            $this->splash('success',null,$this->app->_('该登录账号可用'),true );exit;
        }else{
            $this->splash('error',null,$msg,true);exit;
        }
    }
    //检查法人姓名合法性
  public function check_frname(){
    	
		$flag = $this->userPassport->check_frname($_GET,$msg );
        
        if(!$flag){
          $this->splash('error',null,$msg,true);exit;
        }
    }

 /**
     * 截取文件名不包含扩展名
     * @param 文件全名，包括扩展名
     * @return string 文件不包含扩展名的名字
     */
  private function fileext($filename)
    {
        return substr(strrchr($filename, '.'), 1);
    }
   /**
     * create
     * 创建会员
     * 采用事务处理,function save_attr 返回false 立即回滚
     * @access public
     * @return void
     */
    public function create(){
        //modified by zengxinwen
       
        // $_POST = $_GET;

        // echo "<pre>";
        // var_dump($_POST);
        // exit();
             if ( $_FILES['licensep']['name'] != "" )
        {
            $type=array("png","jpg","gif","jpeg","rar","zip");
            if(!in_array(strtolower($this->fileext($_FILES['licensep']['name'])), $type))
            {
                $text = implode(",", $type);
                $this->end(false, app::get('b2c')->_("您只能上传以下类型文件: ") . $text . "<br>", $com_url,false,$_POST['response_json']);
                $this->ajax_callback('error',app::get('b2c')->_("您只能上传以下类型文件: ") . $text . "<br>");
            }
            
            $mdl_img = app::get('image')->model('image');
            $image_name = $_FILES['licensep']['name'];
            $image_id = $mdl_img->store($_FILES['licensep']['tmp_name'],null,null,$image_name);
            $mdl_img->rebuild($image_id,array('L','M','S'));

            if (isset($_REQUEST['type']))
            {
                $type = $_REQUEST['type'];
            }
            else
            {
                $type = 'xl';
            }
            $image_src = base_storager::image_path($image_id, $type);
        }
        if ( $_FILES['permit']['name'] != "" )
        {
            $type=array("png","jpg","gif","jpeg","rar","zip");
            if(!in_array(strtolower($this->fileext($_FILES['permit']['name'])), $type))
            {
                $text = implode(",", $type);
                $this->end(false, app::get('b2c')->_("您只能上传以下类型文件: ") . $text . "<br>", $com_url,false,$_POST['response_json']);
                $this->ajax_callback('error',app::get('b2c')->_("您只能上传以下类型文件: ") . $text . "<br>");
            }
            
            $mdl_img = app::get('image')->model('image');
            $image_name = $_FILES['permit']['name'];
            $image_id = $mdl_img->store($_FILES['permit']['tmp_name'],null,null,$image_name);
            $mdl_img->rebuild($image_id,array('L','M','S'));

            if (isset($_REQUEST['type']))
            {
                $type = $_REQUEST['type'];
            }
            else
            {
                $type = 'xl';
            }
            $image_src2 = base_storager::image_path($image_id, $type);
        }
      
        $_POST = utils::_filter_input($_POST);
        if($_POST['response_json'] == 'true'){
            $ajax_request = true;
        }else{
            $ajax_request = false;
        }
        if( !$this->userPassport->check_signup($_POST,$msg) ){
            $this->splash('failed',null,$msg,$ajax_request);
        }
        $_POST['licensep'] = $image_src;
        $_POST['permit'] = $image_src2;
    
        $saveData = $this->userPassport->pre_signup_process($_POST);
     //注册时插入默认的办事处office_id
        $office_mdl = app::get('ectools')->model('office');
        $office_id = $office_mdl->getList('office_id',array('default_local' => 1));
        $saveData['b2c_members']['office_id'] = $office_id[0]['office_id'];
        
        if( $member_id = $this->userPassport->save_members($saveData,$msg) ){
            $this->userObject->set_member_session($member_id);
            $this->bind_member($member_id);
            foreach(kernel::servicelist('b2c_save_post_om') as $object) {
                $object->set_arr($member_id, 'member');
                $refer_url = $object->get_arr($member_id, 'member');
            }
            //保存推荐关系
            if(!empty($_SESSION['referrals_code'])){
                $obj_policy = kernel::service("referrals.member_policy");
                if(is_object($obj_policy))
                {
                    $obj_policy ->save_referrals_member($_SESSION['referrals_code'],$member_id);
                }
            }
            //增加会员同步 2012-5-15
            if( $member_rpc_object = kernel::service("b2c_member_rpc_sync") ) {
                $member_rpc_object->createActive($member_id);
            }
            //添加注册积分
            if(!empty($_SESSION['referrals_code'])){
                $obj_policy = kernel::service("referrals.member_policy");
                if(is_object($obj_policy))
                {
                    $obj_policy ->referrals_member($_SESSION['referrals_code'],$member_id);
                }
            }
            /*注册完成后做某些操作! begin*/
            foreach(kernel::servicelist('b2c_register_after') as $object) {
                $object->registerActive($member_id);
            }
            /*end*/
            $data['member_id'] = $member_id;
            $data['uname'] = $saveData['pam_account']['login_account'];
            $data['passwd'] = $_POST['pam_account']['psw_confirm'];
            $data['email'] = $_POST['contact']['email'];
            $data['refer_url'] = $refer_url ? $refer_url : '';
            $data['is_frontend'] = true;
            $obj_account=$this->app->model('member_account');
            $obj_account->fireEvent('register',$data,$member_id);
			
			//logout();
   			$this->unset_member();//注册后取消登录
          if(!strpos($_SESSION['pc_next_page'],'cart')){
                $url = $this->gen_url(array('app'=>'b2c','ctl'=>'wap_member','act'=>'index'));
            }else{
                $url = $_SESSION['pc_next_page'];
            }
			//黄智坤 09-13
		    // $url = 'http://39.108.179.90:8001/index.php/wap/'; 
            //域名备案前使用，域名备案后启用下面的地址
            // $url ='http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].'/index.php/wap/';
            $url ='http://'.$_SERVER['HTTP_HOST'].'/index.php/wap/';
            $this->splash1('success',$url,app::get('b2c')->_('注册申请成功'),true,10,$ajax_request);
        }

        $this->splash('failed',$back_url,app::get('b2c')->_('注册申请失败'),true,10,$ajax_request);
    }

    /*----------- 次要流程 ---------------*/
    public function lost(){
        $this->check_login();
        $this->path[] = array('title'=>app::get('b2c')->_('忘记密码'),'link'=>'a');
        $GLOBALS['runtime']['path'] = $this->path;
        $this->set_tmpl('passport');
        $this->page("wap/passport/lost.html");
    }

       public function retrieve(){
           $this->check_login();
           $username = $_POST['username'];
           $member_id = $this->userObject->get_member_id_by_username($username);

           if(!$member_id){
               $url = $this->gen_url(array('app'=>'b2c','ctl'=>'wap_passport','act'=>'lost'));
               $msg = app::get('b2c')->_('该账号不存在，请检查');
               $this->splash('failed',$url,$msg);
           }

           $pamMemberData = app::get('pam')->model('members')->getList('*',array('member_id'=>$member_id));
           foreach($pamMemberData as $row){
               if($row['login_type'] == 'mobile' && $row['disabled'] == 'false'){
                   $data['mobile'] = $row['login_account'];
                   $verify['mobile'] = true;
               }

               if($row['login_type'] == 'email' && $row['disabled'] == 'false'){
                   $data['email'] = $row['login_account'];
                   $verify['email'] = true;
               }

               if($row['login_type'] == 'local' && $row['disabled'] == 'false'){
                   $data['local'] = $row['login_account'];
               }
           }

           if(!$verify['email'] && !$verify['mobile']){
               $msg = app::get('b2c')->_('由于您并未验证手机或者邮箱，无法自助找回密码，请联系网站客服！');
               $this->splash('failed',$url,$msg);
           }

           if($verify['mobile'] || $verify['email']){
               $this->pagedata['send_status'] = 'true';
           }
           $this->pagedata['site_sms_valide'] = $this->app->getConf('site.sms_valide');
           $this->pagedata['data'] = $data;
           $this->page("wap/passport/lost2.html");
       }


    //发送发送邮件验证码
    public function send_vcode_email()
    {
        $email = $_POST['uname'];
        $type = $_POST['type']; //激活activation

        if( !$type || !$email ){
            $msg = app::get('b2c')->_('请填写正确的邮箱');
            $this->splash('failed',null,$msg,'','',true);
        }

        $login_type = $this->userPassport->get_login_account_type($email);
        if($login_type != 'email'){
            $msg = app::get('b2c')->_('请填写正确的邮箱');
            $this->splash('failed',null,$msg,'','',true);
        }

        if($type == 'reset' && !$this->userPassport->check_signup_account( trim($email),$msg )){
            $this->splash('failed',null,$msg,'','',true);
        }

        $userVcode = kernel::single('b2c_user_vcode');
        if($email){
            $vcode = $userVcode->set_vcode($email,$type,$msg);
        }
        if($vcode){
            //发送邮箱验证码
            $data['vcode'] = $vcode;
            if( !$userVcode->send_email($type,(string)$email,$data) ){
                $msg = app::get('b2c')->_('参数错误');
                $this->splash('failed',null,$msg,'','',true);
            }
        }else{
            $this->splash('failed',null,$msg,'','',true);
        }

        $msg = app::get('b2c')->_('发送成功，请查收验证码邮件并填写验证码。');
        $this->splash('success',null,$msg,'','',true);
    }

    //短信发送验证码
    public function send_vcode_sms(){
        $mobile = $_POST['uname'];
        $type = $_POST['type']; //激活activation

        //这里是验证码
        $this->pagedata['site_sms_valide'] = $this->app->getConf('site.sms_valide');
        if($this->pagedata['site_sms_valide'] == 'true')
        {
            $sms_vcode = $_POST['sms_vcode'];
            if( !isset($sms_vcode) || $sms_vcode == null )
            {
                $this->splash('failed',null,'请输入验证码！', '', '', true);
            }
            if(!base_vcode::verify('b2c',$sms_vcode)){
                $this->splash('failed',null,'验证码输入错误！', '', '',true);
            }
        }

        if( !$type || !$mobile ){
            $msg = app::get('b2c')->_('请填写正确的手机号码');
            $this->splash('failed',null,$msg,'','',true);
        }

        $login_type = $this->userPassport->get_login_account_type($mobile);
        if($login_type != 'mobile'){
            $msg = app::get('b2c')->_('请填写正确的手机号码');
            $this->splash('failed',null,$msg,'','',true);
        }

        if($type=='signup' && !$this->userPassport->check_signup_account( trim($mobile),$msg )){
            $this->splash('failed',null,$msg,'','',true);
        }

        if($type == 'forgot' ){
            $obj_pam_account = app::get('pam')->model('members');
            $mobile_count = $obj_pam_account->count(array('login_account'=>$mobile,'login_type'=>'mobile'));
            if($mobile_count == 0)
            {
                $this->splash('failed',null,app::get('b2c')->_('手机号不存在'), '', '',true);
            }
        }

        $userVcode = kernel::single('b2c_user_vcode');
        if($mobile){
            $vcode = $userVcode->set_vcode($mobile,$type,$msg);
        }
        if($vcode){
            //发送验证码 发送短信
            //logger::info('vcode:'.$vcode);
            $data['vcode'] = $vcode;
            if($type == 'signup')
            {
                $type = 'signup-mobile';
            }
            if( !$userVcode->send_sms($type,(string)$mobile,$data) ){
                $msg = app::get('b2c')->_('发送失败');
                $this->splash('failed',null,$msg,'','',true);
            }
        }else{
            $this->splash('failed',null,$msg,'','',true);
        }
    }


    public function resetpwd_code(){
        $this->check_login();
        $send_type = $_POST['send_type'];
        if(isset($_POST['mobile'])){
            $send_type='mobile';
        }else if(isset($_POST['email'])){
            $send_type='email';
        }
        $userVcode = kernel::single('b2c_user_vcode');
        if( !$vcodeData = $userVcode->verify($_POST[$send_type.'vcode'],$_POST[$send_type],'forgot')){
            $msg = app::get('b2c')->_('验证码错误');
            if($send_type=='email'){
                $msg = app::get('b2c')->_('邮箱验证码错误');
            }else{
                $msg = app::get('b2c')->_('手机验证码错误');
            }
        }
        $data['key'] = $userVcode->get_vcode_key($_POST[$send_type],'forgot');
        $data['key'] = md5($vcodeData['vcode'].$data['key']);
        $data['account'] = $_POST[$send_type];
        $this->pagedata['data'] = $data;
		$_SESSION['error_count']['b2c'][$type]=0;
        $this->display('wap/passport/lost3.html');
    }

    public function resetpassword(){
        $this->check_login();
        $userVcode = kernel::single('b2c_user_vcode');
        $vcodeData = $userVcode->get_vcode($_POST['account'],'forgot');
        $key = $userVcode->get_vcode_key($_POST['account'],'forgot');
        if($_POST['account'] !=$vcodeData['account']  || $_POST['key'] != md5($vcodeData['vcode'].$key) ){
            $msg = app::get('b2c')->_('页面已过期,请重新找回密码');
            $this->splash('failed',null,$msg,'','',true);exit;
        }

          if( !$this->userPassport->check_passport($_POST['login_password'],$_POST['psw_confirm'],$msg) ){
              $this->splash('failed',null,$msg,'','',true);exit;
          }

        $member_id = $this->userObject->get_member_id_by_username($_POST['account']);
        if( !$this->userPassport->reset_passport($member_id,$_POST['login_password']) ){
            $msg = app::get('b2c')->_('密码重置失败,请重试');
            $this->splash('failed',null,$msg,'','',true);
        }
        kernel::single('pam_lock')->flush_lock($member_id);
        $url = $this->gen_url(array('app'=>'b2c','ctl'=>'wap_passport','login'));
        $msg = app::get('b2c')->_('新密码设置成功，请用新密码登录');
        $this->splash('success',$url,$msg,'','',true);
    }

    public function error(){
        $this->unset_member();
        $back_url = $this->gen_url(array('app'=>'b2c','ctl'=>'wap_passport','act'=>'index'));
        $this->_response->set_redirect($back_url)->send_headers();
    }


    public function logout($url){
        if(!$url){
            $url = array('app'=>'wap','ctl'=>'default','act'=>'index','full'=>1);
        }
        $this->unset_member();
        $this->app->model('cart_objects')->setCartNum($arr);
        $this->redirect($url);
    }

    public function unset_member(){
        $auth = pam_auth::instance(pam_account::get_account_type($this->app->app_id));
        foreach(kernel::servicelist('passport') as $k=>$passport){
           $passport->loginout($auth);
        }
        $this->app->member_id = 0;

        //add by 比较了下pc跟wap端，缺少这段代码
        kernel::single('base_session')->set_cookie_expires(0);
        //add end

        $this->cookie_path = kernel::base_url().'/';
        $this->set_cookie('MEMBER',null,time()-3600);
        $this->set_cookie('UNAME','',time()-3600);
        $this->set_cookie('MLV','',time()-3600);
        $this->set_cookie('CUR','',time()-3600);
        $this->set_cookie('LANG','',time()-3600);
        $this->set_cookie('S[MEMBER]','',time()-3600);

        //add by 比较了下pc跟wap端，缺少这段代码
        $this->set_cookie('S[SIGN][AUTO]',0,time()-3600);
        //add end

        foreach(kernel::servicelist('member_logout') as $service){
            $service->logout();
        }
    }

    /*
     * 验证是否登陆
     * */
    public function ajax_check_login(){
        $url = '';
        $status = false;
        if( $this->userObject->is_login() )
        {
            $status = true;
        }else
        {
            $url = $this->gen_url(array('app'=>'b2c', 'ctl'=>'wap_passport','act'=>'login','full'=>1));
        }
        echo json_encode(array('status'=>$status,'url'=>$url));
    }

    //微信自动登陆

    public function wxlogin(){
        
        #$this->pagedata['login_image_url'] = $login_image_url;
        $data = app::get('trustlogin')->getConf('trustlogin_rule');
        //信任登录trustlogin
        if($data['status'] && $data['status'] == 'true')
        {
            foreach(kernel::servicelist('trustlogin_trustlogin.wap_trustlogin_cfg') as $object){
                if(is_object($object)){
                    if(method_exists($object,'get_logo')){
                        $login_image_url[] = $object->get_logo();
                    }
                }
            }

                //信任登录trustlogin判断后台启用状态
            foreach ($login_image_url as $key => $value)
            {
                if(!$value['status'])
                {
                    unset($login_image_url[$key]);
                }
                if($value['status'] && $value['status'] == 'false')
                {
                    unset($login_image_url[$key]);
                }
            }
        }

        $url = 'http://www.thspmall.cn/index.php/wap';
        foreach ($login_image_url as  $value) {
            # code...
            if($value['name'] == '微信' ){
                $url = $value['url'];

            }
        }
        $url = str_replace('snsapi_userinfo', 'snsapi_base', $url);
        header('Location:'.$url);
    }
}
